<?php

namespace App\Service\playlet;
use App\Service\CommicService;
use App\Model\PlayletBlocksRanking;
use App\Model\Applet;

use Hyperf\Utils\ApplicationContext;
use App\Common\Response\SystemCode;
class PlayletBlocksRankingService extends CommicService
{
    const cacheDuration = 60 * 15;
    public function __construct()
    {
        $container = ApplicationContext::getContainer();
        $this->redisService = $container->get(\Hyperf\Redis\Redis::class);
    }
    const STATUS_ENABLE  = 1;

    public function search(Array $params){

        $keyword     = $params['keyword'] ?? '';
        $status      = $params['status'] ?? '';
        $pageSize    = $params['pageSize'] ?? 10;
        $page        = $params['page'] ?? 1;

        $query = PlayletBlocksRanking::query()->select(['id','name','playlet_ids','jump_type','status','template_type','created_at','updated_at']);

        if ($keyword) {
            $query->orWhere([['name', 'like', '%'.$keyword.'%']]);
        }
        if ($status != '') {
            $query->where(['status' => $status]);
        }
        $query->where(['deleted' => 0]);
        $models = $query->orderBy('updated_at','desc')->paginate($pageSize,['*'],'page',$page);
        $items = $models->items();
        $selectData = [];
        foreach ($items as $item){
            $data = $item->toArray();

            $data['status_name']  = self::$commonStatus[$item->status];
            $selectData[] =$data;
        }
        $data = $models->toArray();
        $data['data'] = $selectData;
        return $data;
    }

    public function createOrUpdate(Array $postData,bool $is_update = false,PlayletBlocksRanking $appletBlocksInfo = null){
        $name = $postData['name'];
        $applet_id = $postData['applet_id'] ?? 0;
        $ranking_ids = $postData['ranking_ids'] ?? 0;
        $template_type = $postData['template_type'];
        $status = $postData['status'];
        $model = !empty($appletBlocksInfo)? $appletBlocksInfo : new PlayletBlocksRanking();
        $is_check_template_type = true;
        if($applet_id != 0){
            $appletInfo = Applet::query()->where('id',$applet_id)->first();
            if(empty($appletInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序不存在');
            }
        }


        if($is_update) {
            //修改时判断类型是否一致
            if ($template_type == $model->template_type) {
                $is_check_template_type = false;
            }
        }

        if($is_check_template_type){
            $playletBlocksRankingInfo = PlayletBlocksRanking::query()->where(['applet_id' => $applet_id,'template_type' => $template_type,'deleted' => 0])->first();
            if(!empty($playletBlocksRankingInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序已有此模板');
            }
            $model->template_type = $template_type;
        }

        if($ranking_ids != 0){
            if(!is_array($ranking_ids)){

                return $this->response(SystemCode::SYSTEM_ERROR, 'ranking ID应为数组');
            }

            $select_ranking_ids = PlayletBlocksRanking::query()->whereIN('id',$ranking_ids)->where('deleted',0)->pluck('id')->toArray();
            $diffarray = array_diff($ranking_ids,$select_ranking_ids);

            if(!empty($diffarray)){

                return $this->response(SystemCode::SYSTEM_ERROR, '页面设置中有不存在的数据');
            }
            $ranking_ids = implode(',',$select_ranking_ids);
        }
        $model->name = $name;
        $model->applet_id = $applet_id;
        $model->ranking_ids = $ranking_ids;
        $model->status = $status;

        if($model->save()){
            return $this->response(SystemCode::SYSTEM_SUCCESS);
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR,'保存错误');
        }
    }
}